package com.cloud.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.cloud.common.constants.ExcelPoiConstants;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.docx4j.Docx4J;
import org.docx4j.convert.out.FOSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import java.io.*;
import java.util.Map;

/**
 * 文档操作工具
 *
 * @author yzj
 */
public class ExportUtils {

    /**
     * 根据模板对象导出Word
     *
     * @param document
     */
    public static void wordExport(XWPFDocument document, OutputStream outputStream, File tempFile) {
        try {
            FileOutputStream fileOutPut = new FileOutputStream(tempFile);
            document.write(outputStream);
            fileOutPut.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 强制将缓存区的数据进行输出,清理缓存区数据
                tempFile.deleteOnExit();
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据模板路径导出Excel
     *
     * @param url
     * @param dataMap
     * @param tempFile
     */
    public static void excelExport(String url, Map<String, Object> dataMap, File tempFile) {
        try {
            TemplateExportParams exportParams = new TemplateExportParams(url);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, dataMap);
            FileOutputStream fileOutPut = new FileOutputStream(tempFile);
            workbook.write(fileOutPut);
            fileOutPut.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 强制将缓存区的数据进行输出,清理缓存区数据
            tempFile.deleteOnExit();
        }
    }

    /**
     * 多Sheet模板导出
     * 每个Sheet对应一个map,导出到处,key是Sheet的num
     *
     * @param url
     * @param dataMap
     * @param tempFile
     */
    public static void sheetExcelExport(String url, Map<Integer, Map<String, Object>> dataMap, File tempFile) {
        try {
            TemplateExportParams exportParams = new TemplateExportParams(url);
            Workbook workbook = ExcelExportUtil.exportExcel(dataMap, exportParams);
            FileOutputStream fileOutPut = new FileOutputStream(tempFile);
            workbook.write(fileOutPut);
            fileOutPut.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 强制将缓存区的数据进行输出,清理缓存区数据
            tempFile.deleteOnExit();
        }
    }

    /**
     * 预览Excel转Html
     *
     * @param url
     * @param dataMap
     */
    public static Workbook excelToHtml(String url, Map<String, Object> dataMap) {
        try {
            TemplateExportParams exportParams = new TemplateExportParams(url);
            return ExcelExportUtil.exportExcel(exportParams, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 预览多Sheet_Excel转Html
     *
     * @param url
     * @param dataMap
     */
    public static Workbook sheetExcelToHtml(String url, Map<Integer, Map<String, Object>> dataMap) {
        try {
            TemplateExportParams exportParams = new TemplateExportParams(url);
            return ExcelExportUtil.exportExcel(dataMap, exportParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据模板对象导出Word并转Pdf
     */
    public static void wordToPdfExport(XWPFDocument document, OutputStream outputStream, File tempFile) {
        InputStream is = null;
        try {
            File tempPdfFile = File.createTempFile("temp_", ExcelPoiConstants.Pdf_Suffix);
            is = new FileInputStream(tempPdfFile);
            WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(is);
            Mapper fontMapper = new IdentityPlusMapper();
            fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
            fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
            fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
            fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
            fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
            fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
            fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
            fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
            fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));
            fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
            fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
            fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
            fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
            fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
            mlPackage.setFontMapper(fontMapper);
            FileOutputStream fileOutPut = new FileOutputStream(tempFile);
            document.write(outputStream);
            fileOutPut.close();
            // Word文件转Pdf文件
            FOSettings foSettings = Docx4J.createFOSettings();
            foSettings.setWmlPackage(mlPackage);
            Docx4J.toFO(foSettings, outputStream, Docx4J.FLAG_EXPORT_PREFER_XSL);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 强制将缓存区的数据进行输出,清理缓存区数据
                tempFile.deleteOnExit();
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
